﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Threading;

namespace Translate
{
    public partial class MailForm : Form
    {
        //Thread _btnTra;
        public MailForm()
        {
            InitializeComponent();         
        }
        YouDaoTranslate ydt = new YouDaoTranslate();
        private void btnTra_Click(object sender, EventArgs e)
        {
            //_btnTra = new Thread(new ThreadStart(BtnTra));
            //_btnTra.Start();
            BtnTra();
        }
        /// <summary>
        /// 异步查询
        /// </summary>
        void BtnTra()
        {
            this.BeginInvoke(new Action(() => {
                string froms = txtSourse.Text;
                string[] strfrom = froms.Split('\n');
                txtTager.Text = "";
                if (strfrom.Length == 1)
                {
                    froms = froms.Replace("\n", "").Replace("\r", "");
                    txtTager.Text += ydt.Dictionary(froms.TrimStart().TrimEnd());
                    txtTager.Text += "\r\n";
                }
                else
                {
                    foreach (string str in strfrom)
                    {
                        txtTager.Text += ydt.YDTranslate(str);
                        txtTager.Text += "\r\n";
                    }
                }
            }));
        }
        /// <summary>
        /// 调用有道翻译接口
        /// 需连接上互联网
        /// </summary>
        /// <param name="from">待翻译字符传UTF-8编码，长度小于200</param>
        /// <returns></returns>
        private string Translate(string from)
        {
            if (from.Length >= 200 || from=="") return "";
            string fd = "";
            string strUrl = @"http://fanyi.youdao.com/openapi.do?keyfrom=faw-vm&key=1281620216&type=data&doctype=xml&version=1.1&q="; //获得IP的网址了      
            strUrl += from;
            Uri uri = new Uri(strUrl);
            WebRequest wr = WebRequest.Create(uri);
            wr.Proxy = WebRequest.GetSystemWebProxy();
            wr.Proxy.Credentials = new NetworkCredential("lh_sw_qm", "123456");
            System.IO.Stream s = wr.GetResponse().GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.UTF8);
            string all = sr.ReadToEnd(); //读取网站的数据    
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.LoadXml(all);
            XmlElement root = xmlDoc.DocumentElement;
            foreach (XmlNode xn in root)//遍历所有子节点
            {
                if (xn.Name == "translation")
                {
                    XmlNode ee = xn.ChildNodes.Item(0);
                    fd = ee.FirstChild.InnerText; break;
                }
            }        
            return fd;
        }
        private void MailForm_Load(object sender, EventArgs e)
        {
          // this.Text += SystemInfo.GetWANIP();
        }
    }
}
